<template>
  <div class="validator-container">
    <div class="header">
      <h3>JSON格式验证器</h3>
      <p class="subtitle">验证并加载JSON数据到编辑器</p>
    </div>
    
    <div class="content-wrapper">
      <div class="input-section">
        <label class="input-label">
          <i class="icon-json">{}</i>
          输入JSON数据
        </label>
        <div class="textarea-wrapper">
          <textarea 
            v-model="jsonInput" 
            placeholder="在此粘贴或输入JSON数据..."
            class="json-textarea"
          ></textarea>
          <div class="textarea-actions">
            <span class="char-count">{{ jsonInput.length }} 字符</span>
          </div>
        </div>
        
        <div class="button-group">
          <button @click="validateJson" class="btn btn-primary">
            <i class="btn-icon">✓</i>
            验证格式
          </button>
          <button @click="formatJson" class="btn btn-secondary">
            <i class="btn-icon">🔄</i>
            格式化
          </button>
          <button @click="loadToEditor" class="btn btn-success" :disabled="!isValid">
            <i class="btn-icon">📥</i>
            加载到编辑器
          </button>
          <button @click="clearInput" class="btn btn-danger">
            <i class="btn-icon">🗑️</i>
            清空
          </button>
        </div>
      </div>
      
      <div class="results-section">
        <div v-if="validationResult" :class="['result-card', validationResult.type]">
          <div class="result-header">
            <i :class="['result-icon', validationResult.type]"></i>
            <h4>{{ validationResult.title }}</h4>
          </div>
          <div class="result-content">
            <pre class="result-message">{{ validationResult.message }}</pre>
          </div>
        </div>
        
        <div v-if="parsedData" class="preview-card">
          <div class="preview-header">
            <i class="preview-icon">📊</i>
            <h4>解析预览</h4>
            <span class="preview-count">{{ Array.isArray(parsedData) ? parsedData.length : 'N/A' }} 项</span>
          </div>
          <div class="preview-content">
            <pre class="json-preview">{{ JSON.stringify(parsedData, null, 2) }}</pre>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const jsonInput = ref(`[
  {
    "property": "A",
    "remark": "A",
    "value": [
      {
        "property": "A1",
        "remark": "A1",
        "value": "A1"
      },
      {
        "property": "A2",
        "remark": "A2",
        "value": "A2"
      }
    ]
  },
  {
    "property": "B",
    "remark": "B",
    "value": "B"
  }
]`)

const validationResult = ref(null)
const parsedData = ref(null)
const isValid = computed(() => validationResult.value?.type === 'success')

const validateJson = () => {
  try {
    if (!jsonInput.value.trim()) {
      throw new Error('输入不能为空')
    }
    
    const data = JSON.parse(jsonInput.value)
    parsedData.value = data
    
    // 验证数据结构是否符合键值对格式
    if (!Array.isArray(data)) {
      throw new Error('JSON数据必须是数组格式')
    }
    
    const isValidStructure = data.every(item => 
      typeof item === 'object' && 
      item !== null && 
      'property' in item && 
      'remark' in item && 
      'value' in item
    )
    
    if (!isValidStructure) {
      throw new Error('数据格式不符合键值对要求：每个对象必须包含property、remark、value字段')
    }
    
    validationResult.value = {
      type: 'success',
      title: '验证成功',
      message: `格式正确！共包含 ${data.length} 个键值对项目`
    }
  } catch (error) {
    validationResult.value = {
      type: 'error',
      title: '验证失败',
      message: error.message
    }
    parsedData.value = null
  }
}

const formatJson = () => {
  try {
    const data = JSON.parse(jsonInput.value)
    jsonInput.value = JSON.stringify(data, null, 2)
    validateJson()
  } catch (error) {
    validationResult.value = {
      type: 'error',
      title: '格式化失败',
      message: 'JSON格式错误，无法格式化'
    }
  }
}

const loadToEditor = () => {
  if (isValid.value && parsedData.value) {
    emit('load-data', parsedData.value)
    validationResult.value = {
      type: 'success',
      title: '加载成功',
      message: '数据已成功加载到编辑器'
    }
  }
}

const clearInput = () => {
  jsonInput.value = ''
  validationResult.value = null
  parsedData.value = null
}

const emit = defineEmits(['load-data'])

// 自动验证初始数据
validateJson()
</script>

<style scoped>
.validator-container {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: #fff;
  border-radius: 0.375rem;
  padding: 20px;
  border: 1px solid #e5e7eb;
  max-width: 100%;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h3 {
  margin: 0;
  font-size: 20px;
  color: #334155;
  font-weight: 600;
}

.header .subtitle {
  margin: 8px 0 0;
  color: #64748b;
  font-size: 14px;
}

.content-wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  align-items: start;
}

.input-section {
  background: white;
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.input-label {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  color: #334155;
  margin-bottom: 12px;
  cursor: pointer;
}

.input-label .icon-json {
  font-size: 20px;
  margin-right: 8px;
  color: #4299e1;
}

.textarea-wrapper {
  position: relative;
  margin-bottom: 16px;
}

.json-textarea {
  width: 100%;
  min-height: 200px;
  padding: 12px;
  border: 1px solid #e5e7eb;
  border-radius: 0.25rem;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #334155;
  background-color: #ffffff;
  resize: vertical;
  transition: border-color 0.2s;
}

.json-textarea:focus {
  outline: none;
  border-color: #60a5fa;
  box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.1);
}

.textarea-actions {
  position: absolute;
  bottom: 8px;
  right: 12px;
  font-size: 12px;
  color: #718096;
}

.button-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 20px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.btn-secondary {
  background: linear-gradient(135deg, #ffecd2, #fcb69f);
  color: #2d3748;
}

.btn-success {
  background: linear-gradient(135deg, #84fab0, #8fd3f4);
  color: #2d3748;
}

.btn-danger {
  background: linear-gradient(135deg, #ff9a9e, #fecfef);
  color: #2d3748;
}

.btn-icon {
  margin-right: 8px;
  font-size: 16px;
}

.results-section {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.result-card, .preview-card {
  background: white;
  padding: 24px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.result-card.success {
  border-left: 4px solid #48bb78;
}

.result-card.error {
  border-left: 4px solid #f56565;
}

.result-header, .preview-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.result-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 12px;
}

.result-icon.success {
  background: #48bb78;
}

.result-icon.error {
  background: #f56565;
}

.result-icon::before {
  content: '';
  display: block;
  width: 100%;
  height: 100%;
  background-size: 16px;
  background-repeat: no-repeat;
  background-position: center;
}

.result-icon.success::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='white' viewBox='0 0 24 24'%3E%3Cpath d='M9 12l2 2 4-4'/%3E%3C/svg%3E");
}

.result-icon.error::before {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='white' viewBox='0 0 24 24'%3E%3Cpath d='M6 18L18 6M6 6l12 12'/%3E%3C/svg%3E");
}

.result-header h4, .preview-header h4 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #2d3748;
}

.result-content {
  margin-top: 12px;
}

.result-message {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: #334155;
  background: #ffffff;
  padding: 12px;
  border-radius: 0.25rem;
  font-family: 'Courier New', Courier, monospace;
  border: 1px solid #e5e7eb;
}

.preview-icon {
  font-size: 20px;
  margin-right: 12px;
}

.preview-count {
  margin-left: auto;
  background: #edf2f7;
  color: #4a5568;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.json-preview {
  margin: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #334155;
  background: #ffffff;
  padding: 12px;
  border-radius: 0.25rem;
  border: 1px solid #e5e7eb;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
}

@media (max-width: 768px) {
  .content-wrapper {
    grid-template-columns: 1fr;
    gap: 24px;
  }
  
  .validator-container {
    padding: 20px;
  }
  
  .button-group {
    grid-template-columns: 1fr;
  }
}
</style>